package vip

import (
	"context"

	"github.com/gogf/gf/v2/frame/g"

	"github.com/beherochuling/beesoft_api/app/dao"
	"github.com/beherochuling/beesoft_api/app/helper/net"
	"github.com/beherochuling/beesoft_api/app/model/entity"

	bmodel "github.com/beherochuling/beesoft_api/app/model"
	model "github.com/beherochuling/beesoft_api/app/model/vip"
	"github.com/beherochuling/beesoft_api/app/service/util"
)

var Constant = constant{}

type constant struct{}

func (v *constant) List(ctx context.Context, req *model.ConstantListRequest) (model.ConstantListResponse, int) {
	var result model.ConstantListResponse
	var items []entity.VipConstants

	where := g.Map{}
	util.ParseWhere(req, where)
	// fmt.Println(where)
	// if req.Id != 0 {
	// 	where[dao.VipConstants.Columns().Id] = req.Id
	// }
	// if req.Type != 0 {
	// 	where[dao.VipConstants.Columns().Type] = req.Type
	// }
	// if req.Slug != nil {
	// 	where[dao.VipConstants.Columns().Slug] = req.Slug
	// }
	// if req.Name != "" {
	// 	where[dao.VipConstants.Columns().Name] = req.Name
	// }
	// if req.Title != "" {
	// 	where[dao.VipConstants.Columns().Title] = req.Title
	// }
	// if req.Remark != "" {
	// 	where[dao.VipConstants.Columns().Remark] = req.Remark
	// }
	// if req.CreatedIp != "" {
	// 	where[dao.VipConstants.Columns().CreatedIp] = net.Ipv42Uint32(req.CreatedIp)
	// }
	// if req.CreatedBy != 0 {
	// 	where[dao.VipConstants.Columns().CreatedBy] = req.CreatedBy
	// }
	// if req.CreatedAt.Start != "" {
	// 	where[fmt.Sprintf("%s >= ", dao.VipConstants.Columns().CreatedAt)] = req.CreatedAt.Start
	// }
	// if req.CreatedAt.End != "" {
	// 	where[fmt.Sprintf("%s < ", dao.VipConstants.Columns().CreatedAt)] = req.CreatedAt.End
	// }
	// if req.UpdatedAt.Start != "" {
	// 	where[fmt.Sprintf("%s >= ", dao.VipConstants.Columns().CreatedAt)] = req.UpdatedAt.Start
	// }
	// if req.UpdatedAt.End != "" {
	// 	where[fmt.Sprintf("%s < ", dao.VipConstants.Columns().CreatedAt)] = req.UpdatedAt.End
	// }

	m := dao.VipConstants.Ctx(ctx).
		Where(where)

	count, err := m.Count()
	if err != nil {
		panic(err)
	}

	if count > 0 {
		err := m.Offset((req.Page - 1) * req.PerPage).
			Limit(req.PerPage).
			OrderDesc(dao.VipConstants.Columns().Id).
			Scan(&items)
		if err != nil {
			panic(err)
		}

		for _, item := range items {
			result = append(result, model.ConstantItemResponse{
				model.ConstantItem{
					Id:        item.Id,
					Type:      item.Type,
					Slug:      item.Slug,
					Name:      item.Name,
					Title:     item.Title,
					Remark:    item.Remark,
					CreatedBy: item.CreatedBy,
					CreatedIp: net.Uint2Ipv4(item.CreatedIp),
				},
				bmodel.Time{
					CreatedAt: item.CreatedAt,
					UpdatedAt: item.UpdatedAt,
					DeletedAt: item.DeletedAt,
				},
			})
		}
	}

	return result, count
}
func (v *constant) Show(ctx context.Context, id uint) *model.ConstantShowResponse {
	var item *entity.VipConstants
	err := dao.VipConstants.Ctx(ctx).
		WherePri(id).
		Scan(&item)
	if err != nil {
		panic(err)
	}

	result := &model.ConstantShowResponse{}

	if item != nil {
		result.Id = item.Id
		result.Type = item.Type
		result.Slug = item.Slug
		result.Name = item.Name
		result.Title = item.Title
		result.Remark = item.Remark
		result.CreatedBy = item.CreatedBy
		result.CreatedAt = item.CreatedAt
		result.UpdatedAt = item.UpdatedAt
		result.DeletedAt = item.DeletedAt
		result.CreatedIp = net.Uint2Ipv4(item.CreatedIp)
	}

	return result
}
func (v *constant) Store(ctx context.Context, req *model.ConstantStoreRequest) error {
	_, err := dao.VipConstants.Ctx(ctx).
		Insert(entity.VipConstants{
			Type:      req.Type,
			Slug:      req.Slug,
			Name:      req.Name,
			Title:     req.Title,
			Remark:    req.Remark,
			CreatedIp: net.Ipv42Uint(req.CreatedIp),
			CreatedBy: req.CreatedBy,
		})
	if err != nil {
		panic(err)
	}

	return err
}
func (v *constant) UpdateById(ctx context.Context, id uint, data *model.ConstantUpdateRequest) error { // data *g.Map
	// where := g.Map{}

	data.CreatedIp = net.Ipv42Uint(data.ConstantItem.CreatedIp)
	_, err := dao.VipConstants.Ctx(ctx).
		// Where(where).
		Where(dao.VipConstants.Columns().Id, id).
		Update(data)
	if err != nil {
		panic(err)
	}

	return err
}
func (v *constant) DestroyById(ctx context.Context, id uint) error {
	// where := g.Map{}

	_, err := dao.VipConstants.Ctx(ctx).
		// Where(where).
		Where(dao.VipConstants.Columns().Id, id).
		Delete()
	if err != nil {
		panic(err)
	}

	return err
}
